//
//  FHInputToobar.swift
//  chenChat
//
//  Created by chen on 16/2/21.
//  Copyright © 2016年 chen. All rights reserved.
//

import UIKit
protocol FHInputToolbarDelegate{
    func onInputBtnTapped(text: String)
    func onLeftBtnTapped(text: String)
}

class FHInputToobar: UIView,UITextViewDelegate{

    var backgroundImageView: UIImageView
    var textFieldBackground: UIImageView
    var textField: UITextView
    var leftBtn: UIButton
    var rightBtn: UIButton
    var delegate: FHInputToolbarDelegate?
    override init(frame: CGRect) {
        self.backgroundImageView = UIImageView()
        self.textFieldBackground = UIImageView()
        self.textField = UITextView()
        self.leftBtn = UIButton(type: .Custom)
        self.rightBtn = UIButton(type: .Custom)
       
        super.init(frame: frame)
        self.frame = CGRectMake(frame.origin.x, frame.origin.y, UIScreen.mainScreen().bounds.width, 44)
        self.backgroundImageView.frame = CGRectMake(0, 0, self.frame.width, self.frame.height)
        //拉伸图片
        self.backgroundImageView.image = UIImage.resizableImage("toolbar_bottom_bar.png")
        self.addSubview(self.backgroundImageView)
        //leftBtn
        self.leftBtn.frame = CGRectMake(5, 5, 34, 34)
        self.leftBtn.setBackgroundImage(UIImage(named: "chat_bottom_voice_nor.png"), forState: .Normal)
        self.leftBtn.addTarget(self, action: "onLeftBtnTapped:", forControlEvents: .TouchUpInside)
        addSubview(self.leftBtn)
        
        self.textFieldBackground.frame = CGRectMake(34 + 10, 5, UIScreen.mainScreen().bounds.width - 34 - 10 - 34 - 10, 34)
        self.textFieldBackground.image = UIImage(named: "chat_bottom_textfield.png")
        addSubview(textFieldBackground)
        self.textField.frame = CGRectMake(textFieldBackground.frame.origin.x + 5, 5, textFieldBackground.frame.width, 34)
        self.textField.font = UIFont.systemFontOfSize(17)
        self.textField.backgroundColor = UIColor.clearColor()
        self.textField.delegate = self
        self.addSubview(textField)
        
        self.rightBtn.frame = CGRectMake(UIScreen.mainScreen().bounds.width - 34 - 5, 5, 34, 34)
        self.rightBtn.setBackgroundImage(UIImage(named: "chat_bottom_up_nor.png"), forState: .Normal)
        self.rightBtn.addTarget(self, action: "onRightBtnTapped:", forControlEvents: .TouchUpInside)
        self.addSubview(rightBtn)
        
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func onLeftBtnTapped(sender: AnyObject){
        self.delegate!.onLeftBtnTapped(self.textField.text!)
        self.textField.text = ""
        //这里还需要对他的布局进行还原
        
    }
    func onRightBtnTapped(sender: AnyObject){
        self.delegate!.onInputBtnTapped(self.textField.text!)
        self.textField.text = ""
    }
    func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
        if text == "\n" {
            let number = arc4random()
            if number % 2 == 0 {
                self.onLeftBtnTapped(self.leftBtn)
            }else{
                self.onRightBtnTapped(self.rightBtn)
            }
            
            return false
        }else{
            return true
        }
        
    }
//    func textFieldShouldReturn(textField: UITextField) -> Bool {
//        
//        self.onLeftBtnTapped(self.rightBtn)
//        return true
//    }

}
